<?php

namespace fastflow\driver\think;


use fastflow\lib\config;
use think\Db;
use fastflow\driver\interfaces\workerTemplate;

class worker implements workerTemplate
{
    
    public static function getUsers()
    {
        $userConfig = config::getConfig('user');
        $users = Db::table($userConfig['table'])->field($userConfig['primarykey'] . ',' . $userConfig['username'] . ' as username')->select();
        return $users;
    }

    
    public static function getUser($param)
    {
        if (isset($param)) {
            $userConfig = config::getConfig('user');
            $users = Db::table($userConfig['table'])->field($userConfig['primarykey'] . ',' . $userConfig['username'] . ' as username')->where($param)->select();
            if (count($users) == 0) {
                return false;
            }
            return $users;
        }
        return false;

    }

    
    public static function getGroup($param)
    {
        if (isset($param)){
            $group_config=config::getConfig('group');
            $group_rows=Db::table($group_config['table'])->field($group_config['primarykey'] . ',' . $group_config['groupname'] . ' as groupname,'.$group_config['pid'].' as pid')->where($param)->select();
            if(count($group_rows)==0){
                return false;
            }
            return $group_rows;
        }
        return false;
    }

    
    public static function getGroupAccess($param)
    {
        $groupAaccess_config = config::getConfig('group_access');
        $rows = Db::table($groupAaccess_config['table'])->field($groupAaccess_config['uid'] . ',' . $groupAaccess_config['group_id'])->where($param)->select();
        if (count($rows) == 0) {
            return false;
        }
        return $rows;
    }

    
    public static function getSelectpageWorkers($param)
    {
        $workers = [];
        $total = 0;
        
        if (!isset($param['keyValue'])) {
            
            if ($param['scope'] == '1') {
                $userConfig = config::getConfig('user');
                
                if (isset($param[$param['showField']]) && $param[$param['showField']] != '') {
                    $workers = Db::table($userConfig['table'])
                                 ->where($userConfig['username'], 'like', '%' . $param[$param['showField']] . '%')
                                 ->field($userConfig['primarykey'] . ' as ' . $param['keyField'] . ',' . $userConfig['username'] . ' as ' . $param['showField'])
                                 ->order($userConfig['primarykey'] . ' asc')
                                 ->page($param['pageNumber'], $param['pageSize'])
                                 ->select();
                    $total = Db::table($userConfig['table'])->where($userConfig['username'], 'like', '%' . $param[$param['showField']] . '%')->count();
                } 
                else {
                    $workers = Db::table($userConfig['table'])
                                 ->field($userConfig['primarykey'] . ' as ' . $param['keyField'] . ',' . $userConfig['username'] . ' as ' . $param['showField'])
                                 ->order($userConfig['primarykey'] . ' asc')
                                 ->page($param['pageNumber'], $param['pageSize'])
                                 ->select();
                    $total = Db::table($userConfig['table'])->count();
                }
            } 
            elseif ($param['scope'] == '2') {
                $groupConfig = config::getConfig('group');
                
                if (isset($param[$param['showField']]) && $param[$param['showField']] != '') {
                    $workers = Db::table($groupConfig['table'])
                                 ->where($groupConfig['groupname'], 'like', '%' . $param[$param['showField']] . '%')
                                 ->field($groupConfig['primarykey'] . ' as ' . $param['keyField'] . ',' . $groupConfig['groupname'] . ' as ' . $param['showField'])
                                 ->order($groupConfig['primarykey'] . ' asc')
                                 ->page($param['pageNumber'], $param['pageSize'])
                                 ->select();
                    $total = Db::table($groupConfig['table'])->where($groupConfig['groupname'], 'like', '%' . $param[$param['showField']] . '%')->count();
                } 
                else {
                    $workers = Db::table($groupConfig['table'])
                                 ->field($groupConfig['primarykey'] . ' as ' . $param['keyField'] . ',' . $groupConfig['groupname'] . ' as ' . $param['showField'])
                                 ->order($groupConfig['primarykey'] . ' asc')
                                 ->page($param['pageNumber'], $param['pageSize'])
                                 ->select();
                    $total = Db::table($groupConfig['table'])->count();
                }
            }
            elseif ($param['scope'] == '3' || $param['scope'] == '4') {
                $flow = flow::getFlow(['id' => $param['flow_id']])[0];
                $columns = tool::getFieldsWithComment(['table' => $flow['bill']]);
                foreach ($columns as $column) {
                    $workers[] = [$param['keyField'] => $column['field'], $param['showField'] => $column['field'] . '[' . $column['comment'] . ']'];
                }
                $total = count($workers);
            }
        } 
        else {
            
            if ($param['scope'] == '1') {
                $userConfig = config::getConfig('user');
                $ids = explode(',', $param['keyValue']);
                $workers = Db::table($userConfig['table'])
                             ->where($userConfig['primarykey'], 'in', $ids)
                             ->field($userConfig['primarykey'] . ' as ' . $param['keyField'] . ',' . $userConfig['username'] . ' as ' . $param['showField'])
                             ->select();
            } 
            elseif ($param['scope'] == '2') {
                $groupConfig = config::getConfig('group');
                $ids = explode(',', $param['keyValue']);
                $workers = Db::table($groupConfig['table'])
                             ->where($groupConfig['primarykey'], 'in', $ids)
                             ->field($groupConfig['primarykey'] . ' as ' . $param['keyField'] . ',' . $groupConfig['groupname'] . ' as ' . $param['showField'])
                             ->select();
            }
            elseif ($param['scope'] == '3' || $param['scope'] == '4') {
                $flow = flow::getFlow(['id' => $param['flow_id']])[0];
                $columns = tool::getFieldsWithComment(['table' => $flow['bill']]);
                $fields = explode(',', $param['keyValue']);
                foreach ($fields as $field) {
                    for ($i = 0; $i < count($columns); $i++) {
                        if ($field == $columns[$i]['field']) {
                            $workers[] = [$param['keyField'] => $columns[$i]['field'], $param['showField'] => $columns[$i]['field'] . '[' . $columns[$i]['comment'] . ']'];
                            break;
                        }
                    }
                }

            }
        }
        return ['total' => $total, 'list' => $workers];
    }

}